package com.briup.smartlabs.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.briup.smartlabs.bean.SysMenu;
import com.briup.smartlabs.bean.SysRole;
import com.briup.smartlabs.bean.SysUser;
import com.briup.smartlabs.common.utils.Response;
import com.briup.smartlabs.mapper.SysMenuMapper;
import com.briup.smartlabs.mapper.SysRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "系统菜单信息管理服务")
@RestController
public class SysMenuController {
	
	@Autowired
	SysMenuMapper menuMapper;
	
	@PostMapping("/menu")
	public Response<String> saveOrUpdateMenu(SysMenu menu){
		
		return Response.ok("操作成功！");
	}
	@ApiOperation(value = "返回所有菜单信息")
	@GetMapping("/menu/all")
	public Response<List<SysMenu>> findAll(){
		List<SysMenu> menus = menuMapper.selectAll();
		return Response.ok(menus);
	}
	@ApiOperation(value = "分页显示菜单信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum",value = "当前页码"),
		@ApiImplicitParam(name = "pageSize",value = "每页显示的数据个数")
	})
	@GetMapping("/menu/by/page")
	public Response<PageInfo<SysMenu>> findByPage(
			@RequestParam(defaultValue = "5")int pageSize,
			@RequestParam(defaultValue = "1")int pageNum){
		//表明开始分页，会对接下来代码中遇到的第一条sql语句进行分页查询
		PageHelper.startPage(pageNum, pageSize, true); 
		List<SysMenu> menus = menuMapper.selectAll();
		PageInfo<SysMenu> pages = new PageInfo<>(menus);
		return Response.ok(pages);
	}
	@ApiOperation(value = "按照菜单name删除菜单信息")
	@ApiImplicitParam(name = "name",value = "要删除的菜单的menuName")
	@DeleteMapping("/menu/by/name")
	public Response<String> deleteById(Long name){
		if(name==null) {
			return Response.error("请输入要删除的菜单name！");
		}
		menuMapper.deleteByPrimaryKey(name);
		return Response.ok("删除成功！");
	}
	@ApiOperation(value = "按照菜单名字查找菜单，并分页显示")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name",value = "菜单名"),
		@ApiImplicitParam(name = "pageNum",value = "当前页码",defaultValue = "1"),
		@ApiImplicitParam(name = "pageSize",value = "每页显示的个数",defaultValue = "5")
	})
	@GetMapping("/menu/by/name")
	public Response<PageInfo<SysMenu>> findByName(
			String name,
			@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "5")int pageSize){
		PageHelper.startPage(pageNum, pageSize, true);
		if(name!=null&&!"".equals(name)) {
			name = "%"+name+"%";
		}
		List<SysMenu> menus = ( menuMapper).selectNameLike(name);
		PageInfo<SysMenu> pages = new PageInfo<>(menus);
		return Response.ok(pages);
	}
	
}
